<?php

/**
 * 管理员管理
 * 
 * @author 陈蕾
 */

class Controller_Admin_Admin extends Controller_Admin_Base
{
	// 面板首页
    public function action_default()
    {
        $this->prepare('欢迎进入管理后台');
        $this->content->count = array(
        	'member'  => Model_Member::getCount(),
        	'chapter' => Model_Chapter::getCount(),
        	'lesson'  => Model_Lesson::getCount()
        );
        $this->render();
    }

    // 列表
    public function action_index()
    {
        $count = DB::select(DB::expr("COUNT(*) AS total"))
                ->from('admin')
                ->execute()
                ->get('total');
        $pagination = Pagination::factory(array(
            'total_items' => $count
        ));

        $records = DB::select()->from('admin')
                ->order_by('id', 'DESC')
                ->limit($pagination->items_per_page)
                ->offset($pagination->offset)
                ->execute()->as_array();

        $this->prepare('管理员列表');
        $this->content->records = $records;
        $this->content->pagination = $pagination->render();
        $this->render();
    }

    // 添加
    public function action_add()
    {
        if ($_POST) {
            $post = array(
                'name' => Arr::get($_POST, 'username'),
                'password' => Arr::get($_POST, 'password'),
                'password_confirm' => Arr::get($_POST, 'password_confirm')
            );

            $admin = ORM::factory('admin');
            $admin->values($post);

            /* @var $validate Validate */
            $validate = $admin->validate();
            $validate->rule('password_confirm', 'matches', array('password'))
                ->callback('name', array('Model_Admin', 'username_available'));

            if ( ! $admin->check()) {
                $this->message($validate->errors('admin'));
                return;
            }

            if ( ! $admin->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('添加成功', 'admin');
        } else {
            $this->prepare('新增管理员');
            $this->render();
        }
    }

    // 编辑
    public function action_edit()
    {
        if ($_POST) {
            $id = Arr::get($_POST, 'id');

            $admin = ORM::factory('admin');
            $admin->find($id);
            if ( ! $admin->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }

            // 检查原密码
            $original_password =  Arr::get($_POST, 'original_password');

            if (empty($original_password)) {
                $this->message('请输入原密码！');
                return;
            }

            if ($admin->password != md5($original_password)) {
                $this->message('原密码不正确！');
                return;
            }

            // 检查格式
            $post = array(
                'password' => Arr::get($_POST, 'password'),
                'password_confirm' => Arr::get($_POST, 'password_confirm')
            );
            $admin->values($post);

            $post = $admin->validate()
                ->rule('password_confirm', 'matches', array('password'));

            if ( ! $post->check()) {
                $this->message($post->errors('admin'));
                return;
            }

            // 保存
            if ($admin->save()) {
                $this->message('密码修改成功！', 'admin');
            } else {
                $this->message('操作失败！');
            }
        } else {
            $id = (int)$this->request->param('id');

            if (empty($id)) {
                $this->message('参数错误！');
                return;
            }

            $admin = ORM::factory('admin')->find($id);

            if ( ! $admin->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }

            $this->prepare('修改管理员密码');
            $this->content->record = $admin->as_array();
            $this->render();
        }
    }

    // 删除
    public function action_delete() {
        $id = (int) $this->request->param('id');

        if (empty($id)) {
            $this->message('参数错误！');
            return;
        }

        if ($id == 1) {
            $this->message('编号为 1 的超级管理员不能删除！');
            return;
        }

        $admin = ORM::factory('admin')->find($id);

        if ( ! $admin->loaded()) {
            $this->message('指定的记录不存在！');
            return;
        }

        if ($admin->delete()) {
            $this->message('删除成功！', 'admin');
        } else {
            $this->message('删除失败');
        }
    }
}